const Response = require("../../class/Response");

//控制台聊天显示
WSC.api.listener.on(CONSTANTS.EVENT.PLAYER_CHAT, (json)=>{
    if (CONF.CONFIG.get('Chat')['ConsoleDisplay'] == true){ 
        let sender = json.body.sender;
        let msg = json.body.message;
        let serverName = json.header.source_bds_serverName;
        let time = json.body.time;
        if (mc.getPlayer(sender) != null && !CONF.CONFIG.get('Chat')["ConsoleDisplayLocal"]){
            return;
        }
        WSC.logger.info(`[${serverName}]<${sender}> ${msg}`);
    }
});


mc.listen("onChat", (pl, msg)=>{
    if (!CONF.CONFIG.get('SyncConfig')['Enabled'] || !CONF.CONFIG.get('SyncConfig')['SyncSendOption']['syncChat']){
        return;
    }
    if (pl.isSimulatedPlayer()){return;}
    // if (msg.slice(0, 2) == "./" && ExecutorLevel == 1){
    //     if (!(pl.isOP())){
    //         return; // 执行者不是op则拦截./命令
    //     }
    // }
    // // 自动更改连接执行者
    // if (msg.slice(0, 2) == "./"){
    //     Executor = pl;
    // }
    
    // 原版WS chat json格式
    let res = new Response(Response.MODE.EVENT_PlayerMessage);
    res.body.message = msg;
    res.body.receiver = '';
    res.body.sender = pl.realName;
    res.body.type = 'chat';
    // 本插件WS 额外信息
    res.setSourceId(WSC.client.info.id);
    res.setSourceBdsUuid(CONF.CONFIG.get('Websocket')['BDSUuid']);
    res.setSourceBdsServerName(CONF.CONFIG.get('Websocket')['ServerName']);
    res.body.time = new Date().getTime();
    WSC.client.sendJson(res.json);
    
    // 消息回环 发送到本地其他插件llapi
    WSC.api.listener.emit(CONSTANTS.EVENT.PLAYER_CHAT, {
        header: {
            source_id: WSC.client.info.id,
            source_bds_uuid: CONF.CONFIG.get('Websocket')['BDSUuid'],
            source_bds_serverName: CONF.CONFIG.get('Websocket')['ServerName'],
        },
        body: {
            sender: pl.realName,
            receiver: '',
            message: msg,
            type: 'chat',
            time: res.body.time,
            data: {},
            metaData: {},
            statusCode: 0,
            statusMessage: ''
        }
    });
});


// BDS显示收到的聊天信息(不会收到自己发送的)，显示到游戏内聊天在vanilla命令cmd.js执行, vanilla/chat.js用于拦截原版命令执行, 配置文件中关闭聊天显示。
WSC.onRequestId(CONSTANTS.REQUEST_ID.PLAYERCHAT, (wsclient, req, _res)=>{
    if (!CONF.CONFIG.get('SyncConfig')['Enabled'] || !CONF.CONFIG.get('SyncConfig')['SyncReceiveOption']['syncChat']){
        return;
    }
    //let msg = /^tellraw\s@a\s\{"rawtext":\[\{"text":"(.*)"\}\]\}$/.exec(req.body.commandLine)[1];
    //mc.broadcast(msg, 1); // 这样会有<server>的前缀
    //mc.broadcast(msg); 
    // 已经执行tellraw命令了，不用lse再发送tell聊天消息了。

    // 发送到本地事件
    WSC.api.listener.emit(CONSTANTS.EVENT.PLAYER_CHAT, {
        header: {
            source_id: req.header.source_id,
            source_bds_uuid: req.header.source_bds_uuid,
            source_bds_serverName: req.header.source_bds_serverName,
        },
        body: {
            sender: req.data.sender,
            receiver: '',
            message: req.data.msg,
            type: 'chat',
            time: req.data.time,
            data: {},
            metaData: {},
            statusCode: 0,
            statusMessage: ''
        }
    });
});

